import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'dio_client.dart';
import 'cache_strategy.dart';

void main() {
  runApp(MyApp());
  configLoading();
}

void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.fadingCircle
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow
    ..textColor = Colors.yellow
    ..maskColor = Colors.blue.withOpacity(0.5)
    ..userInteractions = false
    ..dismissOnTap = false;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: EasyLoading.init(),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Dio 封装示例'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () async {
                  try {
                    await DioClient.get(
                      '/data',
                      headers: {'Custom-Header': 'value'},
                      showLoading: false,
                      cacheStrategy: CacheStrategy.cacheFirst,
                      onCacheData: (cacheData) {
                        print('缓存数据: $cacheData');
                      },
                    );
                  } catch (e) {
                    print('请求出错: $e');
                  }
                },
                child: const Text('发送 GET 请求'),
              ),
              ElevatedButton(
                onPressed: () async {
                  try {
                    String filePath = '/path/to/your/file.jpg'; // 请替换为实际文件路径
                    Response response = await DioClient.uploadFile(
                      '/upload',
                      filePath,
                      headers: {'Custom-Header': 'value'},
                    );
                    print(response.data);
                  } catch (e) {
                    print('文件上传出错: $e');
                  }
                },
                child: const Text('上传文件'),
              ),
              ElevatedButton(
                onPressed: () async {
                  try {
                    String url = 'https://example.com/file.pdf'; // 请替换为实际下载链接
                    String savePath = '/path/to/save/file.pdf'; // 请替换为实际保存路径
                    await DioClient.downloadFile(
                      url,
                      savePath,
                      onReceiveProgress: (received, total) {
                        if (total != -1) {
                          print((received / total * 100).toStringAsFixed(0) +
                              '%');
                        }
                      },
                      headers: {'Custom-Header': 'value'},
                    );
                    print('文件下载完成');
                  } catch (e) {
                    print('文件下载出错: $e');
                  }
                },
                child: const Text('下载文件'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
